package com.microstation.service.impl;

import com.microstation.common.Assist;
import com.microstation.common.Result;
import com.microstation.dao.UserInfoDao;
import com.microstation.dao.UserSigninInfoDao;
import com.microstation.entity.po.UserInfo;
import com.microstation.entity.po.UserSigninInfo;
import com.microstation.enums.ResultEnum;
import com.microstation.exception.ElementEmptyException;
import com.microstation.service.UserSigninInfoService;
import com.microstation.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * UserSigninInfo的服务接口的实现类
 *
 * @author
 */
@Service
public class UserSigninInfoServiceImpl implements UserSigninInfoService {
    private final Logger LOG = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private UserSigninInfoDao userSigninInfoDao;
    @Autowired
    private UserInfoDao userInfoDao;

    @Override
    public Result find(Assist assist) {
        //TODO这里可以做通过Assist做添加查询
        long results = userSigninInfoDao.getUserSigninInfoRowCount(assist);
        List<UserSigninInfo> result = userSigninInfoDao.selectUserSigninInfo(assist);
        return new Result(results, result);
    }

    @Override
    public UserSigninInfo findOne(Long id) {
        if (id == null) {
            return null;
        }
        UserSigninInfo result = userSigninInfoDao.selectUserSigninInfoById(id);
        return result;
    }

    @Override
    public Integer saveNotNull(UserSigninInfo value) {
        if (value == null) {
            return null;
        }
        if (value.getUserId() == null || value.getActId() == null) {
            throw new ElementEmptyException(ResultEnum.EMPTY.getInfo());
        }
        int result = userSigninInfoDao.insertNotNullUserSigninInfo(value);
        return result;
    }

    @Override
    public Integer updateNotNullById(UserSigninInfo value) {
        if (value == null) {
            return null;
        }
        int result = userSigninInfoDao.updateNotNullUserSigninInfoById(value);
        return result;
    }

    @Override
    public Integer deleteById(Long id) {
        if (id == null) {
            return null;
        }
        int result = userSigninInfoDao.deleteUserSigninInfoById(id);
        return result;
    }

    @Override
    @Transactional
    public Result updateUserSignin(String ids, long actId) {
        Result result = new Result();
        if (StringUtil.isEmpty(ids)) {
            result.setHasError(true);
            result.setResultEnum(ResultEnum.EMPTY);
            return result;
        }
        String[] idArr = ids.split(",");
        UserSigninInfo userSigninInfo = null;
        for (int i = 0; i < idArr.length; i++) {
            if (StringUtil.isNotEmpty(idArr[i])) {
                userSigninInfo = new UserSigninInfo();
                userSigninInfo.setActId(actId);
                userSigninInfo.setUserId(Long.parseLong(idArr[i]));
                userSigninInfoDao.insertNotNullUserSigninInfo(userSigninInfo);
            }
        }
        result.setHasError(false);
        result.setResultEnum(ResultEnum.SUCCESS);
        return result;
    }

    @Override
    @Transactional
    public Result removeUserInfo(String ids) {
        Result result = new Result();
        if (StringUtil.isEmpty(ids)) {
            result.setHasError(true);
            result.setResultEnum(ResultEnum.EMPTY);
            return result;
        }
        Assist assist = new Assist();
        assist.setRequires(Assist.whereRequire(" and id in (", ")", ids.split(",")));
        UserInfo userInfo = new UserInfo();
        userInfo.setDelFlag(0);
        userInfoDao.updateNotNullUserInfo(userInfo, assist);
        result.setHasError(false);
        result.setResultEnum(ResultEnum.SUCCESS);
        return result;
    }
}